#!/bin/bash
# init script for the openQRM network-manager-plugin
#
# openQRM Enterprise developed by openQRM Enterprise GmbH.
#
# All source code and content (c) Copyright 2014, openQRM Enterprise GmbH unless specifically noted otherwise.
#
# This source code is released under the GNU General Public License version 2, unless otherwise agreed with openQRM Enterprise GmbH.
# The latest version of this license can be found here: src/doc/LICENSE.txt
#
# By using this software, you acknowledge having read this license and agree to be bound thereby.
#
#           http://openqrm-enterprise.com
#
# Copyright 2014, openQRM Enterprise GmbH <info@openqrm-enterprise.com>
#

OPENQRM_SERVER_BASE_DIR=$(pushd $(dirname $0)/../../../../.. > /dev/null; echo $PWD; popd > /dev/null)
. $OPENQRM_SERVER_BASE_DIR/openqrm/etc/openqrm-server.conf
. $OPENQRM_SERVER_BASE_DIR/openqrm/include/openqrm-functions
. $OPENQRM_SERVER_BASE_DIR/openqrm/include/openqrm-server-functions
. $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/network-manager/include/openqrm-plugin-network-manager-functions
. $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/network-manager/etc/openqrm-plugin-network-manager.conf
openqrm_server_get_config
export LANG=C
# define wget to use with https
if [ "$OPENQRM_WEB_PROTOCOL" == "https" ]; then
	WGET_NO_CERT_CHECK="--no-check-certificate"
fi


function openqrm_plugin_device_manager_start() {
	echo "Starting the openQRM network-manager-plugin"
	touch $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/base/plugins/network-manager/.running
}


function openqrm_plugin_device_manager_stop() {
	echo "Stopping the openQRM network-manager-plugin"
	rm -f $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/base/plugins/network-manager/.running
}

function openqrm_plugin_device_manager_uninstall() {
	local USERNAME=$1
	local PASSWORD=$2
	if [ "$USERNAME" == "" ] && [ "$PASSWORD" == "" ]; then
		echo -n "network-manager: Please give a username to uninstall the plugin : "
		read USERNAME
		echo
		echo -n "network-manager: Please give the password for user $USERNAME : "
		read PASSWORD
		echo
	fi
	echo "Uninstalling the openQRM network-manager-plugin"
	openqrm_plugin_device_manager_stop
	rm -f $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/boot-service/boot-service-network-manager.tgz
	rm -f $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/base/plugins/network-manager
}

function openqrm_plugin_device_manager_init() {
	local USERNAME=$1
	local PASSWORD=$2
	if [ "$USERNAME" == "" ] && [ "$PASSWORD" == "" ]; then
		echo -n "network-manager: Please give a username to init the plugin : "
		read USERNAME
		echo
		echo -n "network-manager: Please give the password for user $USERNAME : "
		read PASSWORD
		echo
	fi

	echo "Initializing the openQRM network-manager-plugin"
	# linking the web dir
	ln -sf $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/network-manager/web $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/base/plugins/network-manager
	# link the boot-service
	ln -sf $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/network-manager/web/boot-service-network-manager.tgz $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/boot-service/boot-service-network-manager.tgz
	# create storage dir for stat files
	mkdir -p $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/network-manager/web/storage
	chmod 777 $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/network-manager/web/storage
	echo "Order deny,allow" > $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/network-manager/web/storage/.htaccess
	echo "Deny from all" >> $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/network-manager/web/storage/.htaccess

}







USER=$2
PASS=$3

case "$1" in
	start)
		openqrm_plugin_device_manager_start
		;;
	stop)
		openqrm_plugin_device_manager_stop
		;;
	restart)
		openqrm_plugin_device_manager_stop
		sleep 1
		openqrm_plugin_device_manager_start
		;;
	init)
		openqrm_plugin_device_manager_init $USER $PASS
		;;
	uninstall)
		openqrm_plugin_device_manager_uninstall $USER $PASS
		;;
	*)
		echo "Usage: $0 {start|stop|restart|init|uninstall}"
		exit 1

esac
exit $?




